class CycleCountResponse {
  final int code;
  final List<CycleCountDetail>? data;
  final String msg;
  // 新增分页相关字段（接口返回中存在）
  final int page;
  final int limit;
  final int total;
  final bool isNext;
  final bool isPrevious;

  CycleCountResponse({
    required this.code,
    this.data,
    required this.msg,
    required this.page,
    required this.limit,
    required this.total,
    required this.isNext,
    required this.isPrevious,
  });

  // 修复data列表解析逻辑
  factory CycleCountResponse.fromJson(Map<String, dynamic> json) {
    List<CycleCountDetail>? dataList;
    if (json['data'] != null && json['data'] is List) {
      dataList = (json['data'] as List)
          .map((item) => CycleCountDetail.fromJson(item as Map<String, dynamic>))
          .toList();
    }

    return CycleCountResponse(
      code: json['code'] as int,
      data: dataList,
      msg: json['msg'] as String,
      page: json['page'] as int? ?? 1,
      limit: json['limit'] as int? ?? 10,
      total: json['total'] as int? ?? 0,
      isNext: json['is_next'] as bool? ?? false,
      isPrevious: json['is_previous'] as bool? ?? false,
    );
  }
}

class CycleCountDetail {
  final int id;
  final String? itemNumber;
  final String packKey;
  final String internalItemNumber;
  final String packType;
  final int packTypeQuantity;
  final int? quantity;
  final Warehouse warehouse;
  final Location location;

  CycleCountDetail({
    required this.id,
    this.itemNumber,
    required this.packKey,
    required this.internalItemNumber,
    required this.packType,
    required this.packTypeQuantity,
    this.quantity,
    required this.warehouse,
    required this.location,
  });

  factory CycleCountDetail.fromJson(Map<String, dynamic> json) {
    return CycleCountDetail(
      id: json['id'] as int,
      itemNumber: json['item_number'] as String?,
      packKey: json['pack_key'] as String,
      internalItemNumber: json['internal_item_number'] as String,
      packType: json['pack_type'] as String,
      packTypeQuantity: int.tryParse(json['pack_type_quantity'].toString()) ?? 0, // 处理可能的字符串类型
      quantity: json['quantity'] as int?,
      warehouse: Warehouse.fromJson(json['warehouse'] as Map<String, dynamic>),
      location: Location.fromJson(json['location'] as Map<String, dynamic>),
    );
  }
}

// Location和Warehouse类保持不变
class Location {
  final int id;
  final String number;

  Location({
    required this.id,
    required this.number,
  });

  factory Location.fromJson(Map<String, dynamic> json) {
    return Location(
      id: json['id'] as int,
      number: json['number'] as String,
    );
  }
}

class Warehouse {
  final int id;
  final String name;
  final String code;

  Warehouse({
    required this.id,
    required this.name,
    required this.code,
  });

  factory Warehouse.fromJson(Map<String, dynamic> json) {
    return Warehouse(
      id: json['id'] as int,
      name: json['name'] as String,
      code: json['code'] as String,
    );
  }
}